package com.huadi.firematerialident.controller;

import com.huadi.firematerialident.po.Application;
import com.huadi.firematerialident.po.Appraisal;
import com.huadi.firematerialident.po.FireInfo;
import com.huadi.firematerialident.po.FireMaterialEvidenceInfo;
import com.huadi.firematerialident.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/aps")
public class AppraisalController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    AppraisalServiceImpl appraisalService;
    @Autowired
    IdentifierServiceImpl identifierService;
    @Autowired
    ApplicationServiceImpl applicationService;
    @Autowired
    FireMaterialEvidenceInfoServiceImpl fireMaterialEvidenceInfoService;
    @Autowired
    FireInfoServiceImpl fireInfoService;
    @Autowired
    UserServiceImpl userService;
    @RequestMapping("/apsCommit")
    public String appraisal(
            @RequestParam("AppID") String AppID,
            @RequestParam("ApsConclusion") String ApsConclusion,
            @RequestParam("METype") String METype,
            @RequestParam("FireType") String FireType,
            @RequestParam("FireLevel") String FireLevel,
            @RequestParam("FireTime") String FireTime,
            @RequestParam("FireAdd")String FireAdd,
            Model model
    ) throws ParseException {


        String role = userService.getRole();
        if(!role.equals("identifier")){
            model.addAttribute("msg","您没有权限进行物证鉴定！");
            return "/app/appcommit";
        }

        //判断是否能够根据appID找到MEID，如果找到创建鉴定表，找不到提示出错
        List<Map<String,Object>> list = applicationService.selectApplicationByID(AppID);
        if(list.size() == 0){
            model.addAttribute("msg","请输入正确的申请表ID！");
            return "/ident/applicated";
        }

        // 判断表填写的申请表id是否已经提交过一次申请，如果已经提交过，则报错，如果未提交过，正常提交
        List<Map<String,Object>> list1 = appraisalService.selectAppraisalByID1(AppID);
        if(list1.size()!=0){
            model.addAttribute("msg","该申请表已经提交过鉴定，请勿重复提交！");
            return "/ident/applicated";
        }


        String MEID = (String) applicationService.selectApplicationByID(AppID).get(0).get("MEID");
        String FireInfoID = (String)fireMaterialEvidenceInfoService.selectFireMaterialEvidenceInfoByID(MEID).get(0).get("FireInfoID");

        //创建鉴定表
        Appraisal appraisal = new Appraisal();
        appraisal.setApsID();
        appraisal.setAppID(AppID);
        appraisal.setIdentID(identifierService.getIdentID());
        appraisal.setApsConclusion(ApsConclusion);
        appraisalService.addAppraisal(appraisal);

//        //修改物证表
//        FireMaterialEvidenceInfo fireMaterialEvidenceInfo =fireMaterialEvidenceInfoService.selectFireMaterialEvidenceInfoByID_class(MEID);
//        System.out.println(fireMaterialEvidenceInfo.toString());
//        fireMaterialEvidenceInfo.setMEType(METype);
//        fireMaterialEvidenceInfoService.updateFireMaterialEvidenceInfo(fireMaterialEvidenceInfo);
//
//        //修改火灾信息表
//        LocalDate firetime = LocalDate.parse(FireTime);
//        FireInfo fireInfo = fireInfoService.selectFireInfoByID_class(FireInfoID);
//        fireInfo.setFireType(FireType);
//        fireInfo.setFireLevel(FireLevel);
//        fireInfo.setFireTime(firetime);
//        fireInfo.setFireAdd(FireAdd);
//        fireInfoService.updateFireInfo(fireInfo);

        model.addAttribute("msg","提交成功！");
        return  "/ident/identified";
    }

    @RequestMapping("/update")
    public String updateAppraisal(@RequestParam("ApsID") String apsid,
                                  @RequestParam("AppID") String appid,
                                  @RequestParam("ApsConclusion") String conclu,
                                  @RequestParam("RevStatus") String rstatus,
                                  @RequestParam("ArchivingStatus") String astatus,
                                  Model model){

        //权限检测，只能修改自己的鉴定表
        String IdentID = identifierService.getIdentID();

        Appraisal appraisal = (Appraisal) appraisalService.selectAppraisalByID(apsid);

        if(!appraisal.getIdentID().equals(IdentID)){
            model.addAttribute("msg", "您没有权限修改该表");
            return "/ident/identified";
        }
        appraisal.setAppID(appid);
        appraisal.setApsConclusion(conclu);
        appraisal.setRevStatus(rstatus);
        appraisal.setArchivingStatus(astatus);

        if(appraisalService.updateAppraisal(appraisal)==0){
            model.addAttribute("msg","修改失败！");
        }
        else{
            List<Map<String,Object>> list = appraisalService.selectAppraisalByID(appraisal.getApsID());
            model.addAttribute("msg","修改成功！");
        }
        return "/ident/identified";
    }

    @RequestMapping("/getAllAps")
    @ResponseBody
    public String getAll(Model model){
        List<Map<String,Object>> list = appraisalService.getAllAppraisal();
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/getMyAps")
    @ResponseBody
    public String getMy(Model model){
        List<Map<String,Object>> list = appraisalService.getMyAppraisal();
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/apsSelect")
    @ResponseBody
    public String selectAppraisal(String apsID,Model model){
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from appraisal where ApsID=?",apsID);
        if(list.isEmpty()) {
            model.addAttribute("msg", "没有查找到该表！");
        }
        else{
            return userService.ListToJSON(list.size(),list);
        }
        return null;
    }

}
